/**************************************************************************************** 
 Copyright © 2003-2012 hbasesoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.mingqijia.gassafety.shared.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.gap.basic.exception.BizException;
import org.apache.commons.io.IOUtils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * <Description> <br>
 * 
 * @author 王伟 <br>
 * @version 1.0 <br>
 * @CreateDate 2014年11月5日 <br>
 * @see com.hbasesoft.framework.core.utils <br>
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class IOUtil {


    /**
     * Description: 复制文件<br>
     * 
     * @author 王伟<br>
     * @taskId <br>
     * @param src
     * @param dist <br>
     */
    public static void copyFile(final File src, final File dist) {
        OutputStream out = null;
        InputStream in = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(dist));
            in = new BufferedInputStream(new FileInputStream(src));
            IOUtils.copy(in, out);
        }
        catch (Exception e) {
            throw new BizException("写文件失败!");
        }
        finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }

    /**
     * Description: 复制文件进流<br>
     * 
     * @author 王伟<br>
     * @taskId <br>
     * @param filePath
     * @param in <br>
     */
    public static void copyFileFromInputStream(final String filePath, final InputStream in) {
        copyFileFromInputStream(filePath, in, null);
    }

    /**
     * Description: <br>
     * 
     * @author yang.zhipeng <br>
     * @taskId <br>
     * @param filePath <br>
     * @param in <br>
     * @param charset <br>
     * @throws BizException <br>
     */
    public static void copyFileFromInputStream(final String filePath, final InputStream in, final String charset) {
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(filePath));
            IOUtils.copy(in, out);
        }
        catch (Exception e) {
            throw new BizException("写文件失败!");
        }
        finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }


}
